<?php

include_once('getDBConnection.php');
include_once('FavoriteFood.php');


	class FavoritesService{

		private function getConnection(){
			$obj = new DBConnection();
			return($obj->getDBC());
		}

		/*will save a favorite food to the favoriteFood table based on userId and foodId.  If the food already exists in the table for the given user then only the serving size is updated*/
		function saveFav($userId, $foodId, $servingSize){
			$mysqli = $this->getConnection();

			$userId = $mysqli->real_escape_string($userId);
			$foodId = $mysqli->real_escape_string($foodId);
			$servingSize = $mysqli->real_escape_string($servingSize);

			$selectQuery = "select " .
				Constants::PROFILE_ID . ", " .
				Constants::FOOD_HISTORY_ID .
				" from " .
				Constants::FOOD_FAVORITE .
				" where " .
				Constants::PROFILE_ID . " = $userId " .
				" and " .
				Constants::FOOD_HISTORY_ID . " = $foodId;";

			$rowFound = $mysqli->query($selectQuery);

			if($mysqli->error){
				die("An error occured while trying to query the database for favorite foods: $mysqli->error");
			}

			if(!count($rowFound->fetch_array())){//if the row is not found write an insert query
				$query = "insert into " . Constants::FOOD_FAVORITE . " ( " .
					Constants::PROFILE_ID . ", " .
					Constants::FOOD_HISTORY_ID . ", " .
					Constants::FOOD_FAV_SERVING_SIZE . " ) values ( $userId, $foodId, $servingSize);";
			}else{//if a row is returned then write and update query
				$query = "update " . Constants::FOOD_FAVORITE . " set " .
					Constants::FOOD_FAV_SERVING_SIZE . " = $servingSize " .
					" where " .
					Constants::PROFILE_ID . " = $userId and " .
					Constants::FOOD_HISTORY_ID . " = $foodId;";
			}
			$mysqli->query($query);

			if($mysqli->error){
				die("An error occured while trying to persist a favorite food to the database: $mysqli->error");
			}
			$mysqli->close();
			return($query);
		}

		/*Will delete a single favorite food from the favorite food table based on userId and foodId*/
		function deleteFav($userId, $foodId){
			$mysqli = $this->getConnection();
			$userId = $mysqli->real_escape_string($userId);
			$foodId = $mysqli->real_escape_string($foodId);

			$deleteQuery = "delete from " . Constants::FOOD_FAVORITE .
				" where " .
				Constants::PROFILE_ID . " = $userId " .
				" and " .
				Constants::FOOD_HISTORY_ID . " = $foodId;";

			$mysqli->query($deleteQuery);

			if($mysqli->error){
				die("An error occured while trying to remove a favorite food from the database: $mysqli->error");
			}
			$mysqli->close();
		}

		/*given a userId this function gathers all food in the favorite food table for the user and returns it*/
		function getFav($userId){
			$mysqli = $this->getConnection();
			$userId = $mysqli->real_escape_string($userId);

			$selectQuery = "select fav." . Constants::FOOD_FAV_SERVING_SIZE . ", f.* from " . Constants::FOOD_FAVORITE  . " fav " .
				"left join " . Constants::FOOD_TABLE . " f " .
				"on fav." . Constants::FOOD_HISTORY_ID .
				" = f." . Constants::FOOD_ID .
				" where " .
				Constants::PROFILE_ID . " = $userId;";

			$results = $mysqli->query($selectQuery);

			if($mysqli->error){
				die("An error occured while trying to query the database for favorite foods: $mysqli->error");
			}

			$count=0;

			$return = "<foods>";//start the return values

			while($row = $results->fetch_assoc()){

			$return .=	"<food><id>".$row[Constants::FOOD_ID]."</id>".
						"<name>".$row[Constants::FOOD_NAME]."</name>".
    					"<points>".$row[Constants::FOOD_POINTS]."</points>".
						"<type>".$row[Constants::FOOD_TYPE]."</type>".
						"<calories>".$row[Constants::FOOD_CAL]."</calories>".
						"<fat>".$row[Constants::FOOD_FAT]."</fat>".
						"<fiber>".$row[Constants::FOOD_FIBER]."</fiber>".
						"<servingSize>".$row[Constants::FOOD_FAV_SERVING_SIZE]."</servingSize>".//actual amount of food recorded for a particular mealtime
						"<foodFavServingSize>".$row[Constants::FOOD_FAV_SERVING_SIZE]."</foodFavServingSize>".//actual amount of food recorded for a particular mealtime
						"<isFavorite>true</isFavorite>".//all foods pulled from the favorites table are favorite foods
						"<dateCreated>".$row[Constants::FOOD_DT_CREATED]."</dateCreated>".
						"<dateModified>".$row[Constants::FOOD_DT_MODIFIED]."</dateModified>".
						"<createdBy>".$row[Constants::FOOD_CREATED_BY]."</createdBy></food>";
			}
			$return .= "</foods>";
			$results->close();
			$mysqli->close();
			return($return);
		}
	}

?>